<?php

define( "FUNC_MAIN", 0);
define( "FUNC_SALIR", 1);
define( "FUNC_CAL", 2);
define( "FUNC_ARCH", 3);
define( "FUNC_PERF", 4);
define( "FUNC_AJAX", 10);
define( "FUNC_SHOUT", 20);
define( "FUNC_ADMIN", 30);

require_once '../include/mysql_config.php';
require_once '../include/mysql_connection.php';

require_once '../mvc/main_view.php';

require_once '../mvc/init_session_controller.php';
require_once '../mvc/calendar_controller.php';
require_once '../mvc/archivo_controller.php';
require_once '../mvc/perfil_controller.php';
require_once '../mvc/shoutbox_controller.php';
require_once '../mvc/anuncio_controller.php';
require_once '../mvc/admin_controller.php';

require_once '../mvc/usuario.php';
require_once '../mvc/grupo.php';


class MainController {
	private $main_view;

	private $init_session_controller;
	private $calendar_controller;
	private $archivo_controller;
	private $perfil_controller;
	private $shoutbox_controller;
	private $anuncio_controller;

	private $mysql_connection;

	private $usuario;			// Modelo Usuario


	function __construct() {
		// Utiles
		$this->mysql_connection = new MYSQLConnection( DB_PASS, DB_USER, DB_NAME, DB_SERVER);
		$GLOBALS['mysql_connection'] =& $this->mysql_connection;

		// Modelos
		$this->usuario = new Usuario();

		// Vista
		$this->main_view = new MainView();

		// Controladores
		$this->init_session_controller = new InitSessionController( $this->usuario);
 		$this->calendar_controller = new CalendarController( $this->usuario);
		$this->archivo_controller = new ArchivoController( $this->usuario);
		$this->perfil_controller = new PerfilController( $this->usuario);
		$this->shoutbox_controller = new ShoutboxController( $this->mysql_connection);
		$this->anuncio_controller = new AnuncioController( $this->mysql_connection);
	}

	function display() {
		$this->mysql_connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
		$GLOBALS['mysql_connection'] =& $this->mysql_connection;
		if( $this->mysql_connection->connect_errno) {
			printf( "ERROR al intentar conectar a la base de datos %s\n", $this->mysql_connection_->connect_error);
			exit();
		}

		$func = isset( $_GET['func']) ? $_GET['func'] : FUNC_MAIN;

		if( !$this->init_session_controller->is_log()) {
			if( $func != FUNC_AJAX)
				$this->init_session_controller->display();
			return;
		} 

		switch( $func) {
			default:
			case FUNC_MAIN:
				date_default_timezone_set('America/Santiago');
				$time = time();
				$year = date('Y', $time);
				$month = date('n', $time);
				$today = date('j',$time);

				$is_admin = ( $this->usuario->get_tipo() == "Administrador") ? true : false;

				$this->main_view->display( $this->usuario->get_nickname(), $this->calendar_controller->print_mini_calendar( $year, $month, $today), $this->calendar_controller->print_tittle( $year, $month, $today), $this->shoutbox_controller->display($this->usuario), $this->anuncio_controller->display(), $is_admin );
				break;
			case FUNC_SHOUT:
				if (!empty($_POST['input_msg'])) {
					$msg = $_POST['input_msg'];          
					$this->shoutbox_controller->NewPost($this->usuario->get_id(), $this->usuario->get_id(), $msg);
				}
				break;  
			case FUNC_SALIR:
				$this->init_session_controller->log_out();
				$this->init_session_controller->display();
				break;
			case FUNC_CAL:
				$this->calendar_controller->display();
				break;
			case FUNC_ARCH:
				$this->archivo_controller->display();
				break;
			case FUNC_PERF:
				$this->perfil_controller->display();
				break;
			case FUNC_AJAX:
				break;
			case FUNC_ADMIN:
				$this->admin_controller = new AdminController( $this->usuario);
				$this->admin_controller->display();
				break;
		}
	}
}

?> 
